<?php
defined('IN_XLP') or exit('Access Denied');

$Document = array(
	'pageid'   => 'home-index', //页面标示
	'pagename' => '管理中心', //当前页面名称
	'mycss'	   => array(), //加载的css样式表
	'myjs'	   => array('content/jquery-1.7.2.min'), //加载的js脚本
	'head'	   => true, //加载头部文件
);

include getTpl('header', 'public');
?>
<!--顶部导航 开始-->
<?php include getTpl('top', 'public');?>
<!--顶部导航 结束-->
<!--左侧菜单 开始-->
<?php include getTpl('nav', 'public');?>
<!--左侧菜单 结束-->
<!--主体 开始-->
<!-- content -->
<section id="content">
	<section class="main padder">
		<div class="clearfix">
			<h4><i class="fa fa-table"></i>周边商家管理</h4> 
		</div>
		<section class="panel">
			<div class="panel-body">
				<div class="row">
					<div class="col-lg-12">
						<section class="panel">

							<header class="panel-heading">
							周边商家数据
							</header>

							<div class="panel-body">
								<div class="row text-small">

									<form action="<?php echo U('around/index'); ?>" method="get">
										<div class="col-sm-2">
											<select name="searchtype" id="searchtype" class="form-control">
												<option value="1" <?php if($searchtype == 1) echo 'selected';?>>商户名</option>
												<option value="2" <?php if($searchtype == 2) echo 'selected';?>>电话</option>
												<option value="3" <?php if($searchtype == 3) echo 'selected';?>>地址</option>
											</select>
										</div>
										<div class="col-sm-4">
											<div class="input-group">
												<input type="text" class="input-sm form-control" placeholder="Search" name="keyword" id="keyword" value="<?php echo $keyword; ?>">
												<span class="input-group-btn">
													<button class="btn btn-sm btn-white" type="submit">搜索</button>
												</span>
											</div>
										</div>
									</form>
									
									<div class="m-b-mini" style="margin:-15px 0 0 20px;">
										<a href="javascript:;" id="add_around" class="btn btn-sm btn-white">添加</a>
										<a href="javascript:;" style="display:none;" id="save_around" class="btn btn-sm btn-white">保存</a>
										<a href="javascript:;" style="display:none;" id="cancle_around" class="btn btn-sm btn-white">取消</a>
									</div>

								</div>
							</div>


							<div class="table-responsive">
								<table class="table table-striped b-t text-small">
									<thead>
										<tr>
											<th>ID</th>
											<th>商户名</th>
											<th>全名</th>
											<th>权重</th>
											<th>电话</th>
											<th>地址</th>
											<th>纬度</th>
											<th>经度</th>
											<th>操作</th>
										</tr>
									</thead>
									<tbody id="shop_data">
										<?php foreach ($list as $v): ?>
										<tr id="shop<?php echo $v['id']; ?>">
											<td class="shop_id"><?php echo $v['id']; ?></td>
											<td class="shop_shopName"><?php echo $v['shopName']; ?></td>
											<td class="shop_shopTotalName"><?php echo $v['shopTotalName']; ?></td>
											<td class="shop_shopPower"><?php echo $v['shopPower']; ?></td>
											<td class="shop_phoneNo"><?php echo $v['phoneNo']; ?></td>
											<td class="shop_address"><?php echo $v['address']; ?></td>
											<td class="shop_geoLat"><?php echo $v['geoLat']; ?></td>
											<td class="shop_geoLng"><?php echo $v['geoLng']; ?></td>
											<td>
												<a href="javascript:;" class="del_around" class="btn btn-sm btn-white">删除</a>
											</td>
										</tr>
										<?php endforeach ?>
									</tbody>
								</table>
							</div>

							<footer class="panel-footer"> 
								<div class="row"> 
									<div class="col-sm-12 text-right text-center-sm"> 
										<?php echo page($total,$page,'',20,'page');?>  
									</div> 
								</div> 
							</footer>

						</section>
					</div>
				</div>
			</div>
		</section>
	</section>
	<!--/ main padder -->
</section>
<!--/ content --> 
<!--主体 结束-->
<script>
$(function(){
	var text = '';
	$('#shop_data').dblclick(function(){
		var $target = $(event.target);

		var property = $target.attr('class').substr(5);
		var shop_id  = $target.parent().attr('id').substr(4);
		text = $target.text();

		// alert(property+' '+shop_id+' '+text);
		$target.html('<input autofocus name="'+property+'" type="text" placeholder="'+text+'">');
		$target.children('input').trigger('focus');
	});
	$('#shop_data tr td input').live('blur',function(){
		var $target  = $(event.target);
		var shop_id  = $target.parent().parent().attr('id').substr(4);
		var property = $target.attr('name');
		var value	 = $target.val();
		if(confirm('确定修改此项内容吗？')){
			if($.trim(value)==''){
				alert('内容不能为空');
				$target.parent().html('').text(text);
				return false;
			}
			$.ajax({
				url: '<?php echo U("around/update"); ?>',
				data: {'shop_id':shop_id,'property':property,'value':value},
				type: 'POST',
				dataType: 'JSON',
				success:function(json){
					if(json.status==1){
						$target.parent().html('').text(value);
						Msg.ok(json.info);
					}else{
						$target.parent().html('').text(text);
						Msg.error(json.info);
					}
				},
				error:function(){
					alert('网络异常,请稍候再试');
					$target.parent().html('').text(text);
				}
			});
		}else{
			$target.parent().html('').text(text);
		}
	});

	$('#add_around').click(function(){
		var html = 
			'<form class="around-form"><table class="table table-striped b-t text-small" style="margin-bottom:50px;">\
				<thead>\
					<tr>\
						<th>ID</th>\
						<th>商户名</th>\
						<th>全名</th>\
						<th>权重</th>\
						<th>电话</th>\
						<th>地址</th>\
						<th>纬度</th>\
						<th>经度</th>\
					</tr>\
				</thead>\
				<tbody>\
					<tr>\
						<td></td>\
						<td class="shop_shopName"><input autofocus type="text" name="shopName" /></td>\
						<td class="shop_shopTotalName"><input type="text" name="shopTotalName" /></td>\
						<td class="shop_shopPower"><input type="text" name="shopPower" /></td>\
						<td class="shop_phoneNo"><input type="text" name="phoneNo" /></td>\
						<td class="shop_address"><input type="text" name="address" /></td>\
						<td class="shop_geoLat"><input type="text" name="geoLat" /></td>\
						<td class="shop_geoLng"><input type="text" name="geoLng" /></td>\
					</tr>\
				</tbody>\
			</table></form>';
		$('.table-responsive').prepend(html);
		$(this).hide();
		$('#save_around').show();
		$('#cancle_around').show();
	});

	$('#cancle_around').click(function(){
		$(this).hide();
		$('#save_around').hide();
		$('#add_around').show();
		$('.around-form').hide();
	});

	$('#save_around').click(function(){
		$.post('<?php echo U("around/add"); ?>',$('.around-form').serialize(),function(json){
			if(json.status==1){
				$('#cancle_around').trigger('click');
				Msg.ok(json.info);
			}else{
				Msg.error(json.info);
			}
		},'JSON');
	});

	$('.del_around').click(function(){
		var $this	  = $(this);
		var shop_id   = $this.parent().parent().attr('id').substr(4);
		var shop_name = $this.parent().siblings('.shop_shopName').text();
		if(confirm('真的要删除 '+ shop_name +' 吗？')) {
			$.post('<?php echo U("around/delete"); ?>',{"shop_id":shop_id},function(json){
				if(json.status==1){
					$this.parent().parent().hide();
					Msg.ok(json.info);
				}else{
					Msg.error(json.info);
				}
			},'JSON');
		}
	});

});
</script>
<?php include getTpl('footer', 'public'); ?>